<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增拼团商品列表')" />
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: select2-css" />
</head>

<style>
    .divImg {
        float: left;
        margin: 2px;
        padding: 2px;
        height: 108px;
        width: 100px;
        border: 1px solid #dcdcdc;
        cursor: pointer;
    }
</style>

<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-goodsteam-add">
            <input type="hidden" name="id" id="id" value="">
            <div class="tab-content text-center">
                <div class="tab-pane fade active in" id="base">
                    <div class="row mb10">
                        <div class="col-lg-12">

                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">商品名称：</label>
                                <div class="col-sm-8">
                                    <input name="name" class="form-control" type="text" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">商品副标题：</label>
                                <div class="col-sm-8">
                                    <input name="title" class="form-control" type="text">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label ">主图：</label>
                                <div class="col-sm-8">
                                    <button type="button" class="layui-btn" id="upload_main">上传图片</button>
                                    <div class="layui-upload-list">
                                        <input type="hidden" id="mainUrl" name="mainUrl">
                                        <input type="hidden" id="mainUrlReal" name="mainUrlReal">
                                        <p id="show_main"></p>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><span style="color: red">*</span>商品轮播图</label>
                                <div class="col-sm-8">
                                    <div style="text-align: left;">
                                        <button type="button" class="layui-btn" id="upload_img" >上传图片</button>
                                    </div>
                                    <div class="layui-upload-list">
                                        <div id="img-url" style="padding: 5px;"></div>
                                        <!--<input type="hidden" id="logo" name="logo">-->
                                        <!--<p id="show_img"></p>-->
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><span style="color: red">*</span>商品详情</label>
                                <div class="col-sm-8">
                                    <div style="text-align: left;">
                                        <button type="button" class="layui-btn" id="upload_detailsImg">上传图片</button>
                                    </div>
                                    <div class="layui-upload-list">
                                        <div id="detailsImg-url" style="padding: 5px;"></div>
                                        <!--<input type="hidden" id="logo" name="logo">-->
                                        <!--<p id="show_img"></p>-->
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">默认邮费：</label>
                                <div class="col-sm-8">
                                    <input name="postFee" class="form-control" type="text" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">库存：</label>
                                <div class="col-sm-8">
                                    <input name="stock" class="form-control" type="text" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">原价：</label>
                                <div class="col-sm-8">
                                    <input name="price" class="form-control" type="text" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">拼团价：</label>
                                <div class="col-sm-8">
                                    <input name="priceT" class="form-control" type="text" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">拼团开始时间：</label>
                                <div class="col-sm-8">
                                    <div class="input-group date">
                                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                        <input name="groupStart" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">拼团结束时间：</label>
                                <div class="col-sm-8">
                                    <div class="input-group date">
                                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                        <input name="groupEnd" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">拼团人数：</label>
                                <div class="col-sm-8">
                                    <input name="number" class="form-control" type="text" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">状态：</label>
                                <div class="col-sm-8">
                                    <div class="radio-box" th:each="dict : ${@dict.getType('goods_team_status')}">
                                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status"
                                               th:value="${dict.dictValue}" th:checked="${dict.default}">
                                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">排序：</label>
                                <div class="col-sm-8">
                                    <input name="sort" class="form-control" type="text" required>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
                <div class="tab-pane fade" id="xxcs">
                </div>
            </div>
            <input type="hidden" id="products" name="products">
            <input id="images" name="images" type="hidden">
            <input id="spec" name="spec" type="hidden">
            <input id="param" name="param" type="hidden">
        </form>
    </div>
    <th:block th:include="include :: footer"/>
    <th:block th:include="include :: datetimepicker-js"/>
    <th:block th:include="include :: sortable-js"/>
    <th:block th:include="include :: select2-js" />
    <script type="text/javascript">
        var prefix = ctx + "system/goodsteam"
        $("#form-goodsteam-add").validate({
            focusCleanup: true
        });


        // function submitHandler() {
        //     if ($.validate.form()) {
        //         $.operate.save(prefix + "/add", $('#form-goodsteam-add').serialize());
        //     }
        // }
        $("input[name='createAt']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='groupStart']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            minView: "month",
            autoclose: true
        });

        $("input[name='groupEnd']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            minView: "month",
            autoclose: true
        });

        layui.use('upload', function () {
            var $ = layui.jquery
                , upload = layui.upload;

            //普通图片上传
            upload.render({
                elem: '#upload_main'
                , url: '/common/upload' //改成您自己的上传接口
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#show_main").html("<img src='" + res.url + "' style='width:80px;height:80px;'>");
                    $("#mainUrl").val(res.url);
                    $("#mainUrlReal").val(res.realPath);
                }
            });

            var imgId = 0;
            var container = document.getElementById("img-url");
            var sort = Sortable.create(container);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_img'
                , url: '/common/upload' //改成您自己的上传接口
                , multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name= file.name.split(".")[0];
                        console.log(name)
                        $("#img-url").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgId++;
                    $("#div"+res.name).replaceWith("<div id='imgId" + imgId + "' class='divImg'>" +
                        "<img  onclick=\"setImg('imgId" + imgId + "')\" src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='imgBannerUrl' value='" + res.url + "'>" +
                        "<input type='hidden' name='imgBannerRealPath' value='" + res.realPath + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId" + imgId + "')\"></i></div>");

                    sort.destroy();
                    sort = Sortable.create(container);


                }
            });


            var dimgId = 0;
            var containerD = document.getElementById("detailsImg-url");
            var sortD = Sortable.create(containerD);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_detailsImg'
                , url: '/common/upload' //改成您自己的上传接口
                , multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name= file.name.split(".")[0];
                        console.log(name)
                        $("#detailsImg-url").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    dimgId++;
                    $("#div"+res.name).replaceWith("<div id='imgDetId" + dimgId + "' class='divImg'>" +
                        "<img  onclick=\"setImg('imgId" + dimgId + "')\" src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='imgDetailsUrl' value='" + res.url + "'>" +
                        "<input type='hidden' name='imgDetailsRealPath' value='" + res.realPath + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgDetId" + dimgId + "')\"></i></div>");

                    sort.destroy();
                    sort = Sortable.create(container);

                }
            });


            //普通图片上传
            upload.render({
                elem: '#upload_share_img'
                , url: '/common/upload' //改成您自己的上传接口
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#show_share").html("<img src='" + res.url + "' style='width:80px;height:80px;'>");
                    $("#shareImg").val(res.url);
                }
            });


        });

        function setImg(id) {
            $("#img").find("div").each(function () {
                $(this).removeClass("divImgD").addClass("divImg");
            });
            $("#" + id).removeClass("divImg");
            $("#" + id).addClass("divImgD");
        }

        function delImg(id) {
            $("#" + id).remove();
        }


        function getImg() {
            var imgs = [];

            $("#img-url").find("div").each(function () {
                var img = {};
                $(this).find("img").each(function () {
                    img.url = $(this).attr("src");
                });
                imgs.push(img);
            });
            return imgs;
        }

        function deldetImg(id) {
            $("#" + id).remove();
        }


        function getdetImg() {
            var imgs = [];

            $("#img-url").find("div").each(function () {
                var img = {};
                $(this).find("img").each(function () {
                    img.url = $(this).attr("src");
                });
                imgs.push(img);
            });
            return imgs;
        }


        function submitHandler() {
            if ($.validate.form()) {

                    var specs = [];
                    var spec = "";
                    var price = $("input[name='price']").val();
                    // var priceN = $("input[name='priceN']").val();
                    var priceT = $("input[name='priceT']").val();
                    var stock = $("input[name='stock']").val();
                    var icon = $("input[name='mainUrl']").val();
                    var iconReal = $("input[name='mainUrlReal']").val();
                    var name = $("input[name='name']").val();
                    var isDefault = true;
                    specs.push({
                        'spec': spec,
                        'name': name,
                        'price': price,
                        // 'priceN': priceN,
                        'priceT': priceT,

                        'icon': icon,
                        'iconReal': iconReal,
                        'isDefault': isDefault,
                        'stock': stock
                    });
                    $("#products").val(JSON.stringify(specs));

                var params=[];
                $("#xxcs").find(".form-group").each(function(){
                    var o={};
                    o.name=$(this).find(".btn").text();
                    o.value=$(this).find("input").val();
                    params.push(o);
                    // params.push(obj);
                });
                $("#param").val(JSON.stringify(params));

                $.operate.save(prefix + "/add", $('#form-goodsteam-add').serialize());
            }
        }

        $("#typeId").on("change", function () {
            getType($(this).val());
        });


        function getType(id) {
            getParam(id);
        }

        function getParam(id){
            $.post("/system/goods/getParam" , {"id":id}, function (paramdata) {
                if(paramdata.code==0){
                    var str='';
                    $.each(paramdata.data,function(i,param){
                        str+='<div class="form-group">';
                        // str+='<label class="col-sm-3 control-label">'+param.name+'</label>';

                        str += '<div class="col-sm-12" style="padding-bottom:5px;">' +
                            '<div class="col-sm-10 input-group">' +
                            '<span class="input-group-btn">' +
                            '<button class="btn" style="width:120px;" type="button">'+ param.name+'</button>';
                        str += '</span><input type="text" class="form-control" placeholder="'+ param.name+'"></div></div>';

                        str+='</div>';

                    });
                    $("#xxcs").html(str);
                }else {
                    $("#xxcs").html('');
                }
            }, "json");
        }

    </script>
</body>
</html>